/*
 * UserTask.c
 *
 *      Author: Honrun
 */
#include "gd32f10x.h"
#include "systick.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DevicesDelay.h"
#include "DevicesKey.h"
#include "DevicesLed.h"
#include "DevicesUart.h"
#include "DevicesQueue.h"

#include "main.h"
#include "userTask.h"


TaskHandle_t g_TaskInitHand;           /* 系统初始化任务句柄 */
TaskHandle_t g_TaskReadMessageHand;    /* 读取消息任务句柄 */
TaskHandle_t g_TaskKeyHand;            /* 按键 */


void vTaskReadMessage(void *pvParameters)
{
    static uint8_t st_TaskReadMessageBuff[QUEUE_MAX_LENGTH + 4] = {0};
    int32_t iLength = 0;

    printf("vTaskReadMessage start.\r\n");

    while (1)
    {
        /* 等待消息 */
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

        /* 串口0接收到的数据 */
        while((iLength = iQueueGetLengthOfOccupy(&g_TypeQueueUart0Read)) != 0)
        {
            iLength = iLength < QUEUE_MAX_LENGTH ? iLength : QUEUE_MAX_LENGTH;

            enumQueuePopDatas(&g_TypeQueueUart0Read, st_TaskReadMessageBuff, iLength);

            st_TaskReadMessageBuff[iLength] = 0;
            printf("%s: %s\r\n", g_TypeQueueUart0Read.pcName, st_TaskReadMessageBuff);
        }

        /* 串口4接收到的数据 */
        while((iLength = iQueueGetLengthOfOccupy(&g_TypeQueueUart4Read)) != 0)
        {
            iLength = iLength < QUEUE_MAX_LENGTH ? iLength : QUEUE_MAX_LENGTH;

            enumQueuePopDatas(&g_TypeQueueUart4Read, st_TaskReadMessageBuff, iLength);

            st_TaskReadMessageBuff[iLength] = 0;
            printf("%s: %s\r\n", g_TypeQueueUart4Read.pcName, st_TaskReadMessageBuff);
        }
    }
}

void vTaskKey(void *pvParameters)
{
    KeyTypeDef typeKeyData = {0};

    printf("vTaskKey start.\r\n");

    while (1)
    {
        /* 等待消息 */
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

        /* 按键输入 */
        while(enumQueuePopDatas(&g_TypeQueueKeyInput, (uint8_t *)&typeKeyData, sizeof(KeyTypeDef)) == queueNormal)
        {
            printf("Key state: %04X, valuePress: %04X, timePress: %d, valueLoosen: %04X, timeLoosen: %d\r\n", typeKeyData.state, typeKeyData.valuePress, typeKeyData.timePress, typeKeyData.valueLoosen, typeKeyData.timeLoosen);

            /* 短按键 */
            if((typeKeyData.state == keyShort) && (typeKeyData.valuePress == KEY_RESET))
            {
                printf("keyShort.\r\n");
                printf("valuePress: %02X, state: %04X\r\n", typeKeyData.valuePress, typeKeyData.state);
            }
            /* 长按键 */
            else if((typeKeyData.state == keyLong) && (typeKeyData.valuePress == KEY_RESET))
            {
                printf("keyLong.\r\n");
                printf("valuePress: %02X, state: %04X\r\n", typeKeyData.valuePress, typeKeyData.state);
            }
            /* 持续按键 */
            else if((typeKeyData.state & keyContinuous) && (typeKeyData.valuePress == KEY_RESET))
            {
                printf("keyContinuous.\r\n");
                printf("valuePress: %02X, state: %04X\r\n", typeKeyData.valuePress, typeKeyData.state);
            }
        }
    }
}

void vTaskInit(void *pvParameters)
{
    printf("FreeRTOS start succeed.\r\n");


    /* 新建任务 */
    xTaskCreate(vTaskReadMessage,        "Read Message",       1024, NULL, configMAX_PRIORITIES - 6, &g_TaskReadMessageHand);

    xTaskCreate(vTaskKey,                "Key",                512,  NULL, configMAX_PRIORITIES - 9, &g_TaskKeyHand);


    vUserSystemInit();
    printf("SystemInit succeed.\r\n");

    /* 删除自己 */
    vTaskDelete( NULL );
}
